package com.neusoft.elm_servlet.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm_servlet.dao.OrdersDao;
import com.neusoft.elm_servlet.po.Business;
import com.neusoft.elm_servlet.po.Cart;
import com.neusoft.elm_servlet.po.Food;
import com.neusoft.elm_servlet.po.OrderDetailet;
import com.neusoft.elm_servlet.po.Orders;
import com.neusoft.elm_servlet.util.DBUtil;

public class OrdersDaoImpl implements OrdersDao {

	@Override
	public int createOrders(Orders orders) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("insert into "
				+ " orders values(null,?,?,NOW(),?,?,0)",PreparedStatement.RETURN_GENERATED_KEYS);
		ps.setString(1, orders.getUserId());
		ps.setInt(2, orders.getBusinessId());
		ps.setDouble(3, orders.getOrderTotal());
		ps.setInt(4, orders.getDaId());
		ps.executeUpdate();
		
		ResultSet rs = ps.getGeneratedKeys();
		int orderId = 0;
		while (rs.next()) {
			orderId = rs.getInt(1);
		}
		DBUtil.close(rs, ps);
		return orderId;
	}

	@Override
	public int createOrderDetailet(List<Cart> listCart, int orderId) throws SQLException {
		Connection con = DBUtil.getConnection();
		StringBuffer sql = new StringBuffer("insert into orderdetailet values");
		for (Cart cart:listCart) {
			sql.append("(null,"+orderId+","+cart.getFood().getFoodId()+","+cart.getQuantity()+"),");
		}
		PreparedStatement ps = con.prepareStatement(sql.toString().substring(0, sql.length()-1));
		
		int count = ps.executeUpdate();
		
		DBUtil.close(null, ps);
		
		return count;
	}

	@Override
	public Orders getOrdersById(String orderId) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("SELECT * FROM orders o,business b WHERE o.businessId = b.businessId and o.orderId = ? ");
		ps.setString(1, orderId);
		ResultSet rs = ps.executeQuery();
		Orders orders = null;
		Business business = null;
		while(rs.next()){
			orders = new Orders();
			orders.setUserId(rs.getString("userId"));
			orders.setOrderDate(rs.getString("orderDate"));
			orders.setOrderTotal(rs.getDouble("orderTotal"));
			orders.setDaId(rs.getInt("daId"));
			orders.setOrderState(rs.getInt("orderState"));
			orders.setOrderId(rs.getInt("OrderId"));
			business = new Business();
			business.setBusinessId(rs.getInt("businessId"));
			business.setBusinessName(rs.getString("businessName"));
			business.setBusinessAddress(rs.getString("businessAddress"));
			business.setBusinessExplain(rs.getString("businessExplain"));
			business.setBusinessImg(rs.getString("businessImg"));
			business.setOrderTypeId(rs.getInt("orderTypeId"));
			business.setStarPrice(rs.getDouble("starPrice"));
			business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
			business.setRemarks(rs.getString("remarks"));
			orders.setBusiness(business);
		}
		DBUtil.close(rs, ps, con);
		return orders;
	}

	@Override
	public List<OrderDetailet> getOrderDetailetByOrderId(String orderId) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("select * from orderdetailet od,food f where od.foodId=f.foodId and orderId=?");
		ps.setString(1, orderId);
		ResultSet rs = ps.executeQuery();
		List<OrderDetailet> list = new ArrayList<>();
		while(rs.next()){
			OrderDetailet od = new OrderDetailet();
			Food food = new Food();
			od.setOdId(rs.getInt("odId"));
			od.setQuantity(rs.getInt("quantity"));
			food.setFoodId(rs.getInt("foodId"));
			food.setFoodImg(rs.getString("foodImg"));
			food.setFoodName(rs.getString("foodName"));
			food.setFoodPrice(rs.getDouble("foodPrice"));
			food.setFoodExplain(rs.getString("foodExplain"));
			food.setRemarks(rs.getString("remarks"));
			od.setFood(food);
			list.add(od);
		}
		DBUtil.close(rs, ps, con);
		return list;
	}

	@Override
	public List<Orders> listOrdersByUserId(String userId) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("SELECT * FROM orders o,business b WHERE o.businessId = b.businessId and o.userId = ? ");
		ps.setString(1, userId);
		ResultSet rs = ps.executeQuery();
		List<Orders> list = new ArrayList<>();
		Orders orders = null;
		Business business = null;
		while(rs.next()){
			orders = new Orders();
			orders.setUserId(rs.getString("userId"));
			orders.setOrderDate(rs.getString("orderDate"));
			orders.setOrderTotal(rs.getDouble("orderTotal"));
			orders.setDaId(rs.getInt("daId"));
			orders.setOrderState(rs.getInt("orderState"));
			orders.setOrderId(rs.getInt("OrderId"));
			business = new Business();
			business.setBusinessId(rs.getInt("businessId"));
			business.setBusinessName(rs.getString("businessName"));
			business.setBusinessAddress(rs.getString("businessAddress"));
			business.setBusinessExplain(rs.getString("businessExplain"));
			business.setBusinessImg(rs.getString("businessImg"));
			business.setOrderTypeId(rs.getInt("orderTypeId"));
			business.setStarPrice(rs.getDouble("starPrice"));
			business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
			business.setRemarks(rs.getString("remarks"));
			orders.setBusiness(business);
			list.add(orders);
		}
		DBUtil.close(rs, ps, con);
		return list;
	}


}
